package org.jeecg.modules.demo.posterlevelnewuser.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.demo.moenydata.uitils.GamesEnum;
import org.jeecg.modules.demo.posterlevel.entity.PosterLevelData;
import org.jeecg.modules.demo.posterlevel.service.IPosterLevelService;
import org.jeecg.modules.demo.posterlevelnewuser.service.INewUserPosterLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yw
 * @Date 2021/3/16 16:22
 */
@Api(tags="新用户广告档次分布")
@RestController
@RequestMapping("/newuser/posterlevel")
@Slf4j
public class NewUserPosterLevelController {
    @Autowired
    INewUserPosterLevelService newUserPosterLevelService;
    @AutoLog(value = "广告档次分布-分页列表查询")
    @ApiOperation(value="广告档次分布-分页列表查询", notes="广告档次分布-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PosterLevelData posterLevelData,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   @RequestParam(name = "num",required = false) String num,
                                   HttpServletRequest req) {
        String tbName= GamesEnum.getTableName(num);
        if(tbName.equals("erro")){
            return Result.error("没有对应该参数的库表!");
        }
        tbName+="eventdata";
        posterLevelData.setTableName(tbName);
        Map<String, String[]> parameterMap=req.getParameterMap();
        //循环遍历查询条件
		/*for(String key:parameterMap.keySet()){
			String value = parameterMap.get(key)[0].toString();//
			System.out.println("key:"+key+" vlaue:"+value);
		}*/
        posterLevelData.setPageNo(pageNo*pageSize-pageSize);
        posterLevelData.setPageSize(pageNo*pageSize);
        if(parameterMap.containsKey("system")){
            posterLevelData.setSystem(parameterMap.get("system")[0].toString());
        }
        if(parameterMap.containsKey("begintime")){
            posterLevelData.setBegintime(parameterMap.get("begintime")[0].toString());
        }
        if(parameterMap.containsKey("endtime")){
            posterLevelData.setEndtime(parameterMap.get("endtime")[0].toString());
        }
        Integer total=newUserPosterLevelService.getNewUserDataCount(posterLevelData);
        if(total==0){
            Map<String,Object> map=new HashMap<String,Object>();
            return Result.error("无数据");
        }
        List<PosterLevelData> pageList=getAllList(posterLevelData);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("records",pageList);
        map.put("total",total);
        return Result.OK(map);
    }

    public List<PosterLevelData> getAllList(PosterLevelData posterLevelData){
        List<PosterLevelData> pageList=newUserPosterLevelService.getNewUserPosterList(posterLevelData);//数据集合
        List<PosterLevelData> levelList=newUserPosterLevelService.getNewUserLevelList(posterLevelData);//广告档次集合
        for(int i=0;i<pageList.size();i++){
            for(int j=0;j<levelList.size();j++){
                if(pageList.get(i).getCtime().equals(levelList.get(j).getCtime())){
                    PosterLevelData data=levelList.get(j);
                    pageList.get(i).setPosterYes(data.getPosterYes());
                    pageList.get(i).setPosterNo(pageList.get(i).getUserCount()-pageList.get(i).getPosterYes());
                    pageList.get(i).setRank1(data.getRank1());
                    pageList.get(i).setRank2(data.getRank2());
                    pageList.get(i).setRank3(data.getRank3());
                    pageList.get(i).setRank4(data.getRank4());
                    pageList.get(i).setRank5(data.getRank5());
                    pageList.get(i).setRank6(data.getRank6());
                    pageList.get(i).setRank7(data.getRank7());
                    pageList.get(i).setRank8(data.getRank8());
                    pageList.get(i).setRank9(data.getRank9());
                    pageList.get(i).setRank10(data.getRank10());
                    pageList.get(i).setRank11(data.getRank11());
                    pageList.get(i).setRank12(data.getRank12());
                }
            }

        }
        return  pageList;
    }
}
